// ignore_for_file: use_key_in_widget_constructors, file_names, no_logic_in_create_state, must_be_immutable, annotate_overrides, overridden_fields, avoid_print, slash_for_doc_comments

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mybase/base/super/FFInk.dart';
import 'package:mybase/base/super/FFState.dart';
import 'package:mybase/base/super/FFStatefulWidget.dart';
import 'package:mybase/base/tool/platformUtils/FFPlatform.dart';

/**点击控件 */
class FFGestureDetectorIOS extends FFStatefulWidget {
  /**注释说明 */
  String? notes;

  /**点击事件 */
  Function? onClick;

  /**长按事件*/
  Function()? onLongPress;

  /**是否用UnconstrainedBox，默认false
   * 当设置为true,容器会自动撑到实际大小
   * 设置为false,宽高优先来自自身的宽高值，其次是父级约束
   */
  bool? unconstrainedBox;

  /**对齐方式 */
  AlignmentGeometry? alignment;

  /**外边框 */
  EdgeInsetsGeometry? margin;

  /**内边框 */
  EdgeInsetsGeometry? padding;

  /**背景颜色 */
  Color? color;

  /**装饰 */
  Decoration? decoration;

  /**宽 */
  double? width;

  /**高 */
  double? heigth;

  /**隐藏组件，但保留位置，默认值为false */
  bool? hiddenKeepFrame;

  /**隐藏组件，并清除位置，默认值为false,优先级低于hiddenKeepFrame */
  bool? hiddenClearFrame;

  /**重复点击最少间隔，毫秒，默认值600*/
  int? minClickIntervalMill;

  /**占满主方向的空间**/
  bool? isExpand = false;

  // 忽略本身的交互
  bool? ignoreInteraction;

  // 忽略本身及子组件的交互
  bool? ignoreInteractionAll;

  FFGestureDetectorIOS({
    Key? key,
    this.notes,
    this.onClick,
    this.onLongPress,
    this.unconstrainedBox = false,
    //
    this.alignment = Alignment.topLeft,
    this.margin,
    this.padding,
    this.color,
    this.decoration,
    this.width,
    this.heigth,
    this.hiddenKeepFrame = false,
    this.hiddenClearFrame = false,
    this.minClickIntervalMill = 600,
    this.isExpand = false,
    this.ignoreInteraction = false,
    this.ignoreInteractionAll = false,
  }) : super(key: key);
}

class FFGestureDetectorIOSState extends FFState {
  @override
  FFGestureDetectorIOS get widget => super.widget as FFGestureDetectorIOS;

  int? lastMill; //上一次点击时间

  /**父类处理点击事件 */
  @override
  Widget build(BuildContext context) {
    Widget? w = normal(context);

    if (widget.ignoreInteraction == true) {
      w = IgnorePointer(
        ignoring: true, // 设置为 true 则禁用交互
        child: w,
      );
    } else if (widget.ignoreInteractionAll == true) {
      w = AbsorbPointer(
        absorbing: true, // 设置为 true 则禁用交互
        child: w,
      );
    }

    Widget hiddenWid = w;
    if (widget.hiddenKeepFrame == true) {
      hiddenWid = Opacity(
        opacity: widget.hiddenKeepFrame == true ? 0 : 1,
        child: w,
      );
    } else if (widget.hiddenClearFrame == true) {
      hiddenWid = Offstage(child: w, offstage: widget.hiddenClearFrame ?? false);
    }

    // 放到最后
    if (widget.isExpand == true) {
      hiddenWid = Expanded(child: hiddenWid);
    }

    return hiddenWid;
  }

  Widget normal(BuildContext context) {
    Widget wid = Container(
      alignment: widget.alignment,
      width: widget.width,
      height: widget.heigth,
      margin: widget.margin,
      padding: widget.padding,
      color: widget.decoration == null ? widget.color : null,
      decoration: widget.decoration,
      child: childBuild(context),
    );

    Widget ges = GestureDetector(
      behavior: HitTestBehavior.translucent,
      onLongPress: widget.onLongPress != null
          ? () {
              if (widget.hiddenKeepFrame == true || widget.hiddenClearFrame == true) {
                //隐藏状态不能点击
              } else {
                if (widget.onLongPress != null) {
                  repeatedClickJudgment(widget.onLongPress);
                }
              }
            }
          : null,
      onTapDown: (e) {
        onTapDown(e);
      },
      onTapCancel: () {
        onTapCancel();
      },
      onTapUp: (e) {
        onTapUp(e);
        if (widget.hiddenKeepFrame == true || widget.hiddenClearFrame == true) {
          //隐藏状态不能点击
        } else {
          if (widget.onClick != null) {
            repeatedClickJudgment(() {
              widget.onClick!();
            });
          }
        }
      },
      child: widget.unconstrainedBox == true
          ? UnconstrainedBox(
              child: wid,
            )
          : wid,
    );

    // 网页可点击的手形
    bool canClick = widget.onClick != null && widget.hiddenClearFrame != true && widget.hiddenKeepFrame != true;
    if (FFPlatform.isWeb && canClick) {
      ges = MouseRegion(
        cursor: SystemMouseCursors.click,
        child: ges,
      );
    }

    return ges;
  }

  onTapDown(TapDownDetails e) {}

  onTapUp(TapUpDetails e) {}

  onTapCancel() {}

  /**子类布局用这个 */
  Widget childBuild(BuildContext context) {
    return Container();
  }

  ///重复点击判断
  repeatedClickJudgment(Function? fun) {
    if (fun != null) {
      if (isBeyondClickInterval()) {
        fun();
      }
    }
  }

  bool isBeyondClickInterval() {
    int currentMill = DateTime.now().millisecondsSinceEpoch;
    if (lastMill == null) {
      lastMill = currentMill;
      return true;
    }

    int interval = currentMill - (lastMill ?? 0);
    if (interval >= (widget.minClickIntervalMill ?? 600)) {
      lastMill = currentMill;
      return true;
    }

    debugPrint("最少时间间隔${widget.minClickIntervalMill}不满足");
    return false;
  }
}
